﻿//-----------------------------------------------------------------------
// <copyright file="NhRepository.cs" company="OverrideThis">
//     Copyright (c) 2010 OverrideThis.com
// </copyright>
//-----------------------------------------------------------------------
namespace OverrideThis.Core.Infrastucture.Repository.Impl
{
    using System;
    using System.Linq;
    using NHibernate;
    using NHibernate.Linq;

    public class NhRepository : IRepository
    {
        private readonly ISession session;

        public NhRepository(ISession session)
        {
            this.session = session;
        }

        #region Implementation of IRepository

        public void SaveOrUpdate<T>(T entity)
        {
            this.session.SaveOrUpdate(entity);
        }

        public void Delete<T>(T entity)
        {
            this.session.Delete(entity);
        }

        public void WithTransaction(Action<IRepository> action)
        {
            using(var tran = this.session.BeginTransaction())
            {
                action(this);
                tran.Commit();
            }
        }

        public IQueryable<T> Get<T>()
        {
            return this.session.Linq<T>();
        }

        #endregion
    }
}